// pages/login.js
const app = getApp()
const domain = app.globalData.domain
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: "admin",
    password: "admin123",
    dialogVis: false,
    currentTenant: null,
    options: [{
        value: '1',
        label: '测试租户1'
      },
      {
        value: 2,
        label: '测试租户2'
      },
    ],
  },
  changeTenant(e) {
    console.log('changeTenant', e)
    this.setData({
      currentTenant: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (wx.getStorageSync('token')) {
      wx.reLaunch({
        url: '../operation/index',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  tenantLogin() {
    console.log(' this.data', this.data)
    if (!this.data.currentTenant) {
      wx.showToast({
        title: '请选择租户',
        icon: 'none'
      })
      return
    }
    wx.request({
      url: `${domain}/admin-api/system/auth/login`,
      method: 'POST',
      header: {
        'tenant-id': this.data.currentTenant
      },
      data: {
        username: this.data.username,
        password: this.data.password,
        skipDecrypt: true
      },
      success: (res) => {
        console.log(res, res?.data)
        if (res?.data?.data?.accessToken) {
          wx.setStorageSync('tenant', this.data.currentTenant)
          wx.setStorageSync('token', res?.data?.data?.accessToken)
          wx.redirectTo({
            url: '/pages/operation/index',
          })
        } else {
          wx.showToast({
            title: res?.data?.message,
            icon: 'none'
          })
        }
      }
    })
  },
  login() {
    console.log(' this.data', this.data)
    wx.request({
      url: `${domain}/admin-api/system/auth/login`,
      method: 'POST',
      header: {
        'tenant-id': app.globalData['tenant-id']
      },
      data: {
        username: this.data.username,
        password: this.data.password,
        skipDecrypt: true
      },
      success: (res) => {
        console.log(res, res?.data)
        if (res?.data?.data?.accessToken) {
          wx.setStorageSync('token', res?.data?.data?.accessToken)
          wx.redirectTo({
            url: '/pages/operation/index',
          })
        } else {
          wx.showToast({
            title: res?.data?.message,
            icon: 'none'
          })
        }
      }
    })
  },
  phoneLogin(e) {
    console.log('phoneLogin', e)
    const {
      code: mobileCode,
      encryptedData: mbEncryptedData,
      iv: mbIv
    } = e?.detail
    wx.login({
      success: (res) => {
        const {
          code
        } = res
        wx.getUserInfo({
          success: function (res) {
            console.log('userInfo', res)
            const {
              encryptedData,
              iv,
              rawData
            } = res
            wx.request({
              url: `${domain}/auth/wxmalogin`,
              method: 'POST',
              header: {
                'tenant-id': app.globalData['tenant-id']
              },
              data: {
                mbEncryptedData,
                mbIv,
                loginCode: code,
                mobileCode,
                encryptedData,
                iv,
                rawData
              },
              success: (res) => {
                console.log(res)
                if (res?.data?.token) {
                  wx.setStorageSync('token', res?.data?.token)
                  wx.redirectTo({
                    url: '/pages/operation/index',
                  })
                } else {
                  wx.showToast({
                    title: res?.data?.message,
                    icon: 'none'
                  })
                }
              }
            })
          }
        })
        console.log(res)

      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})